<template>
  <div class="demoList">
    <Loading v-if="total == null"></Loading>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>Demo管理</el-breadcrumb-item>
      <el-breadcrumb-item>Demo列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索Demo区域 -->
      <el-row>
        <el-col :span="8"
          ><el-input
            placeholder="请输入标题关键字查询"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getDemoList"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="getDemoList"></el-button>
            </template> </el-input
        ></el-col>
      </el-row>

      <el-table :data="demoList" border stripe class="mt10">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column
          prop="title"
          label="Demo标题"
          align="left"
          header-align="center"
        >
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="350px" align="center">
          <template #default="scope">
            <el-tag
              v-for="(tag, i) in scope.row.tags"
              :key="tag._id"
              :type="type[i]"
              style="margin: 3px 5px"
            >
              {{ tag.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="190px" align="center">
          <template #default="scope">
            {{ formatTime(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              @click="router.push(`/demo/edit/${scope.row._id}`)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="removeDemo(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getCurrentInstance,
  onBeforeMount,
  reactive,
  toRefs,
} from "@vue/runtime-core";
import { useRouter } from "vue-router";
import { getData, deleteData } from "../../services/http";
import dayjs from "dayjs";
export default {
  name: "demoList",
  setup() {
    const { proxy } = getCurrentInstance();
    const router = useRouter();
    const type = ["primary", "success", "info", "warning"];
    const state = reactive({
      demoList: [],
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5,
      },
      total: null,
    });
    const stateAsRefs = toRefs(state);
    const getDemoList = async () => {
      const { data: res } = await getData("rest/demo", {
        params: state.queryInfo,
      });
      state.demoList = res.data;
      state.total = res.total;
    };

    const removeDemo = (row) => {
      proxy
        .$confirm(`确定要删除 ${row.title}吗？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async () => {
          await deleteData(
            `rest/demo/${row._id}`,
            `已成功删除Demo ${row.title}`
          );
          getDemoList();
        });
    };
    const formatTime = (time) => {
      return dayjs(time).format("YYYY年MM月DD日HH时mm分");
    };
    const handleCurrentChange = (newPage) => {
      state.queryInfo.pageNum = newPage;
      getDemoList();
    };
    const handleSizeChange = (newSize) => {
      state.queryInfo.pageSize = newSize;
      getDemoList();
    };
    onBeforeMount(() => {
      getDemoList();
    });
    return {
      ...stateAsRefs,
      router,
      removeDemo,
      formatTime,
      handleCurrentChange,
      handleSizeChange,
      getDemoList,
      type,
    };
  },
};
</script>
